package com.cloud.drore.eboss.operation.service.impl;

import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.entity.base.UserInfo;
import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerPo;
import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerVo;
import com.cloud.drore.eboos.common.entity.operation.po.FinaceSourseDataCommonPo;
import com.cloud.drore.eboos.common.entity.operation.vo.FinaceSourseDataListVo;
import com.cloud.drore.eboos.common.entity.operation.vo.FinaceSourseDataVo;
import com.cloud.drore.eboos.common.entity.operation.vo.FinaceSourseDistributorDataVo;
import com.cloud.drore.eboos.common.util.*;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.operation.entity.*;
import com.cloud.drore.eboss.operation.entity.po.*;
import com.cloud.drore.eboss.operation.entity.vo.*;
import com.cloud.drore.eboss.operation.mapper.DistributorFormMapper;
import com.cloud.drore.eboss.operation.mapper.DistributorMapper;
import com.cloud.drore.eboss.operation.mapper.FinaceOperationMapper;
import com.cloud.drore.eboss.operation.mapper.FinaceSettlementHistoryMapper;
import com.cloud.drore.eboss.operation.service.IFeignFinace;
import com.cloud.drore.eboss.operation.service.IFinaceSettlementHistoryService;
import com.cloud.drore.eboss.supplier.mapper.SupplierSettlementAutoInfoMapper;
import com.cloud.drore.eboss.util.DictionaryMapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

import java.util.stream.Collectors;

import static com.cloud.drore.eboos.common.constant.RedisConstant.OTA_CASH_POOL;
import static com.cloud.drore.eboos.common.constant.RedisConstant.TRAVEL_CASH_POOL;


/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/31 12:45 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */

@Service
@Slf4j
public class FinaceSettlementHistoryServiceImpl extends BaseServiceImpl implements IFinaceSettlementHistoryService {
    @Resource
    FinaceFormOperationServiceImpl finaceFormOperationServiceImpl;
    @Resource
    FinaceSettlementHistoryMapper finaceSettlementHistoryMapper;
    @Resource
    SupplierSettlementAutoInfoMapper supplierSettlementAutoInfoMapper;
    @Resource
    DistributorMapper distributorMapper;
    @Autowired
    FinaceOperationMapper finaceOperationMapper;
    @Resource
    IFeignFinace iFeignFinace;
    @Resource
    DistributorFormMapper distributorFormMapper;
    @Autowired
    RedisKeysClockUtil redisKeysClockUtil;

    @Override
    public MyMapper getMapper() {
        return finaceSettlementHistoryMapper;
    }

    private static final int OPERATION_FEGIN_SERVICE_SUCCESS_CODE = 8200;

    /**
     * @param
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: java.lang.Long
     * @Decription: 初始化结算历史记录表:包括结算期和结算历史记录表的id，自动结算的时候该方法进行调用（手动）
     * @CreateDate: Created in 2018/1/31 0031 上午 9:59
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Transactional
    public void initFinaceSettlementHistory(List<SupplierSettlementInfoVo> supplierSettlementInfo) {
        List<FinaceSettlementHistoryOperation> returnResultList = new ArrayList<>();
        List<FinaceSourseDataCommonPo> finaceSourseDataCommonPoList = new ArrayList<>();
        //根据供应商id查询结算标识表来判断是否进行过结算，取得上一次的结算时间
        // List<SupplierSelectHistoryRecenetTimeVo> supplierSelectHistoryRecenetTimeList = finaceSettlementHistoryMapper.querySupplierSettlementInfo();
        for (SupplierSettlementInfoVo supplierSettlementInfoVo : supplierSettlementInfo) {
            /**建立eboss_finace_settlement_history*/
            FinaceSettlementHistoryOperation returnResult = buildFinaceSettlementHistory(supplierSettlementInfoVo);
            returnResultList.add(returnResult);
            /** eboss_finace_form*/
            log.info("建立的历史记录表为：" + returnResult);
            FinaceSourseDataCommonPo finaceSourseDataCommonPo = buildFinaceForm(returnResult, supplierSettlementInfoVo);
            finaceSourseDataCommonPoList.add(finaceSourseDataCommonPo);
        }
        //到分销商那边的服务获取到订单产品结算的信息
        ResultMessage<FinaceSourseDataListVo> finaceSourseDataOperationList =
                finaceFormOperationServiceImpl.finaceSourseDataOperationList(finaceSourseDataCommonPoList);
        //供应商报表
        List<FinaceSourseDataVo> finaceSourseDataOperations =
                finaceSourseDataOperationList.getData().getSupplierList();
        //分销商报表
        List<FinaceSourseDistributorDataVo> finaceSourseDataDealerList =
                finaceSourseDataOperationList.getData().getDistributorList();
        //批量插入数据到财务报表对象的表中
        if (finaceSourseDataOperations.size() == 0 || finaceSourseDataDealerList.size() == 0) {
            throw new RuntimeException("当前供应商没有可以用来结算的账单！");
        }
        finaceOperationMapper.insertList(finaceSourseDataOperations);
        distributorFormMapper.insertList(finaceSourseDataDealerList);


        //供应商得到应结算的总额
        List<FinaceSourseDataVo> supplierList = finaceSourseDataOperationList.getData().getSupplierList();
        for (FinaceSourseDataVo finaceSourseDataVo : supplierList) {
            for (FinaceSettlementHistoryOperation finaceSettlementHistoryOperation : returnResultList) {
                if (finaceSettlementHistoryOperation.getMerchantId() == finaceSourseDataVo.getSupplierId()) {
                    //计算供应商的本期总的需要结算的价格
                    FinaceSettlementHistoryOperation finaceSettlementHistoryOperationFinally =
                            rebuildFinaceSettlementHistoryOperation(finaceSourseDataOperationList.getData().getSupplierList(), finaceSourseDataVo.getSupplierId());
                    //设置自动应结算的总额
                    finaceSettlementHistoryOperation.setTotalAmountIncome(finaceSettlementHistoryOperationFinally.getTotalAmountIncome());
                    finaceSettlementHistoryOperation.setTotalAmountShould(finaceSettlementHistoryOperationFinally.getTotalAmountShould());
                    //更改历史记录表
                    updateByPrimaryKey(finaceSettlementHistoryOperation);
                }
            }
        }
        //分销商结算(涉及到流水：远程调用，考虑事务的回滚性放到最后)
        settlementFundDealer(finaceSourseDataOperationList.getData().getDistributorList());
    }


    /**
     * @param
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: java.lang.Long
     * @Decription: 初始化结算历史记录表:包括结算期和结算历史记录表的id，自动结算的时候该方法进行调用（自动）
     * @CreateDate: Created in 2018/1/31 0031 上午 9:59
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Transactional
    public void AutoinitFinaceSettlementHistory(List<SupplierSettlementInfoVo> supplierSettlementInfo) {
        List<FinaceSettlementHistoryOperation> returnResultList = new ArrayList<>();
        List<FinaceSourseDataCommonPo> finaceSourseDataCommonPoList = new ArrayList<>();
        //根据供应商id查询结算标识表来判断是否进行过结算，取得上一次的结算时间
        for (SupplierSettlementInfoVo supplierSettlementInfoVo : supplierSettlementInfo) {
            /**建立eboss_finace_settlement_history*/
            FinaceSettlementHistoryOperation returnResult = buildFinaceSettlementHistoryAuto(supplierSettlementInfoVo);
            returnResultList.add(returnResult);
            /** eboss_finace_form*/
            FinaceSourseDataCommonPo finaceSourseDataCommonPo = buildFinaceFormAuto(returnResult, supplierSettlementInfoVo);
            finaceSourseDataCommonPoList.add(finaceSourseDataCommonPo);
        }
        //到分销商那边的服务获取到订单产品结算的信息
        ResultMessage<FinaceSourseDataListVo> finaceSourseDataOperationList =
                finaceFormOperationServiceImpl.finaceSourseDataOperationList(finaceSourseDataCommonPoList);
        //供应商报表
        List<FinaceSourseDataVo> finaceSourseDataOperations =
                finaceSourseDataOperationList.getData().getSupplierList();
        //分销商报表
        List<FinaceSourseDistributorDataVo> finaceSourseDataDealerList =
                finaceSourseDataOperationList.getData().getDistributorList();
        //批量插入数据到财务报表对象的表中
        if (finaceSourseDataOperations.size() == 0 || finaceSourseDataDealerList.size() == 0) {
            throw new RuntimeException("当前供应商没有可以用来结算的账单！");
        } else {
            //维护自动结算需要的字段
            LocalDate mldt = LocalDate.of(LocalDate.now().getYear(), LocalDate.now().getMonth().plus(1), 5);
            finaceSettlementHistoryMapper.updateSupplierSettlementInfo(DateToStringUtil.getStringDate(LocalDateAndDateTransformUtil.localDateToDate(mldt)));
        }
        finaceOperationMapper.insertList(finaceSourseDataOperations);
        distributorFormMapper.insertList(finaceSourseDataDealerList);


        //供应商得到应结算的总额
        List<FinaceSourseDataVo> supplierList = finaceSourseDataOperationList.getData().getSupplierList();
        for (FinaceSourseDataVo finaceSourseDataVo : supplierList) {
            for (FinaceSettlementHistoryOperation finaceSettlementHistoryOperation : returnResultList) {
                if (finaceSettlementHistoryOperation.getMerchantId() == finaceSourseDataVo.getSupplierId()) {
                    //计算供应商的本期总的需要结算的价格
                    FinaceSettlementHistoryOperation finaceSettlementHistoryOperationFinally =
                            rebuildFinaceSettlementHistoryOperation(finaceSourseDataOperationList.getData().getSupplierList(), finaceSourseDataVo.getSupplierId());
                    //设置自动应结算的总额
                    finaceSettlementHistoryOperation.setTotalAmountShould(finaceSettlementHistoryOperationFinally.getTotalAmountShould());
                    finaceSettlementHistoryOperation.setTotalAmountIncome(finaceSettlementHistoryOperationFinally.getTotalAmountIncome());
                    //更改历史记录表
                    updateByPrimaryKey(finaceSettlementHistoryOperation);
                }
            }
        }
        //分销商结算(涉及到流水：远程调用，考虑事务的回滚性放到最后)
        settlementFundDealer(finaceSourseDataOperationList.getData().getDistributorList());
    }

    /**
     * @param
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.entity.operation.po.FinaceSourseDataCommonPo
     * @Decription: 建立eboss_finace_form（手动）
     * @CreateDate: Created in 2018/4/11 0011 下午 4:36
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSourseDataCommonPo buildFinaceForm(FinaceSettlementHistoryOperation returnResult, SupplierSettlementInfoVo supplierSettlementInfoVo) {
        //开始从分销商服务那边获取结算的记录
        FinaceSourseDataCommonPo finaceSourseDataCommonPo = new FinaceSourseDataCommonPo();
        //设置po的属性
        finaceSourseDataCommonPo.setFinaceSettlementHistoryId(returnResult.getId());
        finaceSourseDataCommonPo.setSettlementStartTime(returnResult.getSettlementStartTime());
        finaceSourseDataCommonPo.setSettlementEndTime(returnResult.getSettlementEndTime());
        //获取当前供应商id
        finaceSourseDataCommonPo.setSupplierId(supplierSettlementInfoVo.getSupplierId());
        return finaceSourseDataCommonPo;
    }


    /**
     * @param
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.entity.operation.po.FinaceSourseDataCommonPo
     * @Decription: 建立eboss_finace_form（自动）
     * @CreateDate: Created in 2018/4/11 0011 下午 4:36
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSourseDataCommonPo buildFinaceFormAuto(FinaceSettlementHistoryOperation returnResult, SupplierSettlementInfoVo supplierSettlementInfoVo) {
        //开始从分销商服务那边获取结算的记录
        FinaceSourseDataCommonPo finaceSourseDataCommonPo = new FinaceSourseDataCommonPo();
        //设置po的属性
        finaceSourseDataCommonPo.setFinaceSettlementHistoryId(returnResult.getId());
        finaceSourseDataCommonPo.setSettlementEndTime(returnResult.getSettlementEndTime());
        finaceSourseDataCommonPo.setSettlementStartTime(returnResult.getSettlementStartTime());
        //获取当前供应商id
        finaceSourseDataCommonPo.setSupplierId(supplierSettlementInfoVo.getSupplierId());
        return finaceSourseDataCommonPo;
    }


    /**
     * @param supplierSettlementInfo
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: void
     * @Decription: 建立eboss_finace_settlement_history（手动）
     * @CreateDate: Created in 2018/4/11 0011 下午 4:20
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation buildFinaceSettlementHistory(SupplierSettlementInfoVo supplierSettlementInfo) {
        /** eboss_finace_settlement_history*/
        Long supperId = supplierSettlementInfo.getSupplierId();
        //根据供应商的id得到当前结算期数的拼装,更新供应商结算表的订单历史记录
        FinaceSettlementHistoryOperation finaceSettlementHistoryOperation = finaceSettlementHistory(supperId, supplierSettlementInfo.getSettlementDate());
        finaceSettlementHistoryOperation.setMerchantId(supperId);
        finaceSettlementHistoryOperation.setSettlementType("线下结算");
        /** 本次结算时间，结算之前的单子*/
        finaceSettlementHistoryOperation.setSettlementEndTime(supplierSettlementInfo.getSettlementDate());
        finaceSettlementHistoryOperation.setAutoFlag(1);
        /** 下一次结算时间*/
        LocalDateTime nextSettlementTime = LocalDateAndDateTransformUtil.dateToLocalDateTime(supplierSettlementInfo.getSettlementDate()).plusMonths(1);
        finaceSettlementHistoryOperation.setSettlementNextTime(LocalDateAndDateTransformUtil.localDateTimeToDate(nextSettlementTime));
        //返回id，用于订单报表关联主键的插入
        FinaceSettlementHistoryOperation returnResult = (FinaceSettlementHistoryOperation) insert(finaceSettlementHistoryOperation);
        return returnResult;
    }


    /**
     * @param supplierSettlementInfo
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: void
     * @Decription: 建立eboss_finace_settlement_history（自动）
     * @CreateDate: Created in 2018/4/11 0011 下午 4:20
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation buildFinaceSettlementHistoryAuto(SupplierSettlementInfoVo supplierSettlementInfo) {

        /** eboss_finace_settlement_history*/
        Long supperId = supplierSettlementInfo.getSupplierId();
        //根据供应商的id得到当前结算期数的拼装,更新供应商结算表的订单历史记录
        FinaceSettlementHistoryOperation finaceSettlementHistoryOperation = autofinaceSettlementHistory(supperId, supplierSettlementInfo.getSettlementDate());
        finaceSettlementHistoryOperation.setSettlementType("线下结算");
        finaceSettlementHistoryOperation.setMerchantId(supperId);
        /** 本次结算时间，结算之前的单子*/
        finaceSettlementHistoryOperation.setSettlementEndTime(supplierSettlementInfo.getSettlementDate());
        finaceSettlementHistoryOperation.setAutoFlag(0);
        /** 下一次结算时间*/
        LocalDateTime nextSettlementTime = LocalDateAndDateTransformUtil.dateToLocalDateTime(supplierSettlementInfo.getSettlementDate()).plusMonths(1);
        finaceSettlementHistoryOperation.setSettlementNextTime(LocalDateAndDateTransformUtil.localDateTimeToDate(nextSettlementTime));
        //返回id，用于订单报表关联主键的插入
        FinaceSettlementHistoryOperation returnResult = (FinaceSettlementHistoryOperation) insert(finaceSettlementHistoryOperation);
        return returnResult;
    }


    /**
     * @param supplierSettlementInfo
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: void
     * @Decription: 自动结算成功修改异常结算数据的状态
     * @CreateDate: Created in 2018/4/3 0003 下午 1:41
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */

    public void updateSupplierState(SupplierSettlementInfoVo supplierSettlementInfo) {
        //当前供应商的结算时间
        Date currentSettlementDate = supplierSettlementInfo.getSettlementDate();
        //下个月
        LocalDate nextSettlementDate = LocalDateAndDateTransformUtil.dateToLocalDate(currentSettlementDate).plusMonths(1);
        supplierSettlementAutoInfoMapper.updateSettlementDate(supplierSettlementInfo.getSupplierId(),
                DateToStringUtil.getStringDate(LocalDateAndDateTransformUtil.localDateToDate(nextSettlementDate)));
        log.info("供应商id：" + supplierSettlementInfo.getSupplierId() + " 下个月结算时间修改成功！");
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: 通过供应商id和结算期查询运营商的结算记录
     * @CreateDate: Created in 2018/3/20 0020 下午 6:38
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<FinaceSettlementHistoryOperation>> queryFinaceSettlementHistoryTotalList
    (PagePo<FinaceSourseOperationTotalPo> page) {
        FinaceSourseOperationTotalPo finaceSourseOperationTotalCommonPo = page.getFields();
        Example finaceSourseDataOperationExample = new Example(FinaceSettlementHistoryOperation.class);
        Example.Criteria criteria = finaceSourseDataOperationExample.createCriteria();
        //结算期
        if (StringUtils.isNotEmpty(finaceSourseOperationTotalCommonPo.getSettlementStage())) {
            criteria.andEqualTo("settlementStage", finaceSourseOperationTotalCommonPo.getSettlementStage());
        }

        criteria.andEqualTo("merchantId", finaceSourseOperationTotalCommonPo.getSupplierId());
        criteria.andEqualTo("isDeleted", 0);
        finaceSourseDataOperationExample.orderBy("createdDate");
        PageHelper.startPage(page);
        List<FinaceSettlementHistoryOperation> finaceSourseDataOperationsList =
                finaceSettlementHistoryMapper.selectByExample(finaceSourseDataOperationExample);
        PageInfo pageInfo = new PageInfo<>(finaceSourseDataOperationsList);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param finaceSourseDataOperationList
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: void
     * @Decription: 自动结算的时候和分销商的账户进行结算
     * @CreateDate: Created in 2018/2/25 0025 下午 7:11
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public void settlementFundDealer(List<FinaceSourseDistributorDataVo> finaceSourseDataOperationList) {
        Map flagMap = new HashMap();
        //初始值设定
        flagMap.put(1, 0);
        flagMap.put(2, 0);
        FinaceSourseDistributorDataVo end = new FinaceSourseDistributorDataVo();
        end.setDistributorId(0L);
        end.setId(0L);
        finaceSourseDataOperationList.add(end);
        List<FinaceSourseDistributorDataVo> collect = finaceSourseDataOperationList.stream()
                .sorted((u1, u2) -> u2.getId().compareTo(u1.getId())).collect(Collectors.toList());

        log.info("分销商开始结算");
        List<FinaceFlowMeterPo> finaceFlowMeterPoList = new ArrayList<>();
        for (FinaceSourseDistributorDataVo finaceSourseDataOperation : collect) {
            if ((flagMap.get(1).toString().equals(finaceSourseDataOperation.getDistributorId().toString()))
                    || (flagMap.get(1).toString().equals("0"))) {
                //相等则将该分销商的总额和之前相加存入到2里面
                BigDecimal totalAmountSettlementOta = finaceSourseDataOperation.getTotalAmountSettlementOta();
                if (null == totalAmountSettlementOta || "".equals(totalAmountSettlementOta)) {
                    flagMap.put(1, finaceSourseDataOperation.getDistributorId());
                    flagMap.put(2, new BigDecimal("0").add(new BigDecimal(flagMap.get(2).toString())));
                } else {
                    flagMap.put(1, finaceSourseDataOperation.getDistributorId());
                    flagMap.put(2, totalAmountSettlementOta.add(new BigDecimal(flagMap.get(2).toString())));
                }
            } else {
                //不相等则对分销商账户进行结算操作，同时清空map重新放入数据
                //查询得到当前分销商账户的余额并进行相减
                DistributorForRedisInfoVo distributorForRedisInfoVo = distributorMapper.selectDistributorAccountAmount(Long.parseLong(flagMap.get(1).toString()));
                if (!(null == distributorForRedisInfoVo || "".equals(distributorForRedisInfoVo))) {
                    BigDecimal bigDecimal = distributorForRedisInfoVo.getAccountBalance();
                    if (!(null == bigDecimal || "".equals(bigDecimal))) {
                        distributorMapper.updateDistributorAccountAmount
                                (bigDecimal.subtract(new BigDecimal(flagMap.get(2).toString())), Long.parseLong(flagMap.get(1).toString()));
                    }
                }
                log.info("分销商 " + flagMap.get(1) + " 应结算的金额为" + flagMap.get(2).toString());
                //分销商结算生成流水属性设定
                DistributorRecharge distributorRecharge = new DistributorRecharge();
                distributorRecharge.setDistributorId(Long.parseLong(flagMap.get(1).toString()));
                distributorRecharge.setAccountArrivalAmount(new BigDecimal(flagMap.get(2).toString()));
                //这里需要先握住流水生成的数据
                FinaceFlowMeterPo finaceFlowMeterPo = produceRechargeFlow(distributorRecharge);
                finaceFlowMeterPoList.add(finaceFlowMeterPo);

                flagMap.clear();
                flagMap.put(1, finaceSourseDataOperation.getDistributorId());
                flagMap.put(2, finaceSourseDataOperation.getTotalAmountSettlementOta());

                /*redis同步分销商的账户信息*/
                //通过属性拿到对应的key
               /* if (!(null == distributorForRedisInfoVo || "".equals(distributorForRedisInfoVo))) {
                    String key = judgeOtaOrHousekeeper(distributorForRedisInfoVo);
                    redisKeysClockUtil.cashPoolChange(key, distributorRecharge.getAccountArrivalAmount(), false);
                }*/
            }
            //如果分销商id为0则表示计算周期已经结束
            if (finaceSourseDataOperation.getDistributorId() == 0) {
                break;
            }
        }
        //正真的流水生成
        ResultMessage resultMessage = iFeignFinace.produceAccountRechargeFlowMeter(finaceFlowMeterPoList);
        if (resultMessage.getCode() != OPERATION_FEGIN_SERVICE_SUCCESS_CODE) {
            throw new RuntimeException("远程调用插入流水失败！");
        }
    }

    /**
     * @param distributorForRedisInfoVo
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: java.lang.String
     * @Decription: 通过商户属性得到对应的key值
     * @CreateDate: Created in 2018/3/24 0024 上午 11:40
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String judgeOtaOrHousekeeper(DistributorForRedisInfoVo distributorForRedisInfoVo) {
        String key;
        //判断性质
        if (distributorForRedisInfoVo.getMerchantIndustry().equals("MERCHANT_INDUSTRY_OTA")) {
            key = distributorForRedisInfoVo.getMerchantNum() + OTA_CASH_POOL;
        } else if (distributorForRedisInfoVo.getMerchantIndustry().equals("MERCHANT_INDUSTRY_HOUSEKEEPER")) {
            key = distributorForRedisInfoVo.getMerchantNum() + TRAVEL_CASH_POOL;
        } else {
            throw new RuntimeException("redis的key值获取失败！");
        }
        return key;
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: 通过供应商的id查询得到运营商版本资金结算数据
     * @CreateDate: Created in 2018/3/20 0020 下午 6:36
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<FundSettlementVo>> queryFundSettlementList(PagePo<FundSettlementPo> page) {
        PageHelper.startPage(page);
        List<FundSettlementVo> fundSettlementVos = finaceSettlementHistoryMapper.queryFundSettlement(page.getFields());
        Long total = new PageInfo(fundSettlementVos).getTotal();
        //供应商结算的周期的一些设定
        List<FundSettlementVo> fundSettlementList = finaceSettlementHistoryMapper.querySettlementFlagInfo();
        //渲染前段需要加载的页面
        for (FundSettlementVo vos : fundSettlementVos) {
            for (FundSettlementVo list : fundSettlementList) {
                if (vos.getMerchantId() == list.getMerchantId()) {
                    vos.setSettlementCycle(list.getSettlementCycle());
                    vos.setSettlementType(list.getSettlementType());
                    LocalDateTime localDateTime = LocalDateAndDateTransformUtil.dateToLocalDateTime(list.getNextAutoSettlementTime());
                    vos.setNextAutoSettlementTime(LocalDateAndDateTransformUtil.localDateTimeToDate(localDateTime.plusMonths(1)));
                }
            }
        }
        PageInfo pageInfo = new PageInfo<>(fundSettlementVos);
        pageInfo.setTotal(total);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param id
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: java.util.List<com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation>
     * @Decription: 运营商结算记录通过供应商的id查询结算期下拉列表
     * @CreateDate: Created in 2018/3/20 0020 下午 6:37
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public List<FinaceSettlementHistoryOperation> queryStageOperationList(Long id) {
        return finaceSettlementHistoryMapper.queryStageOperationList(id);
    }

    /**
     * @param distributorRecharge
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.po.FinaceFlowMeterPo
     * @Decription: 分销商结算生成流水
     * @CreateDate: Created in 2018/3/20 0020 上午 9:17
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceFlowMeterPo produceRechargeFlow(DistributorRecharge distributorRecharge) {
        FinaceFlowMeterPo finaceFlowMeterPo = new FinaceFlowMeterPo();
        Date date = LocalDateAndDateTransformUtil.localDateTimeToDate(LocalDateTime.now());
        finaceFlowMeterPo.setDistributorId(distributorRecharge.getDistributorId());  //分销商id
        finaceFlowMeterPo.setEventAmount(distributorRecharge.getAccountArrivalAmount());//交易金额
        finaceFlowMeterPo.setEventType(0);//事件类型：0账户 1订单
        finaceFlowMeterPo.setFlowNum(CodingGeneration.getFlowGenerationCodingNum("ACCOUNT_EVENT_SETTLEMENT")); //流水号
        finaceFlowMeterPo.setOrderEventStatus("ACCOUNT_EVENT_SETTLEMENT");//事件状态
        finaceFlowMeterPo.setOrderProduceTime(date);//时间
        finaceFlowMeterPo.setCreatedDate(date);
        return finaceFlowMeterPo;
    }

    /**
     * @param finaceSourseDataOperationList
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation
     * @Decription: 查询订单产品表结束之后得到List<FinaceSourseDataOperation>，需要对所有结算单价进行累加
     * @CreateDate: Created in 2018/2/2 0002 下午 3:55
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation rebuildFinaceSettlementHistoryOperation(List<FinaceSourseDataVo> finaceSourseDataOperationList,
                                                                                    Long SupplierId) {
        BigDecimal totalAmountShould = new BigDecimal("0");
        BigDecimal totalAmountIncome = new BigDecimal("0");
        //对财务订单报表返回的list的数据里面的应结算字段进行累加
        for (FinaceSourseDataVo r : finaceSourseDataOperationList) {
            if (r.getSupplierId() == SupplierId) {
                totalAmountShould = totalAmountShould.add(r.getTotalAmountSettlementSupplier());
            }
        }
        //对财务订单报表返回的list的数据里面的收入总额字段进行累加
        FinaceSettlementHistoryOperation finaceSettlementHistoryOperation = new FinaceSettlementHistoryOperation();
        finaceSettlementHistoryOperation.setTotalAmountShould(totalAmountShould);
        finaceSettlementHistoryOperation.setTotalAmountIncome(totalAmountIncome);
        return finaceSettlementHistoryOperation;
    }

    /**
     * @param
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.vo.FundSettlementTotalVo
     * @Decription: 资金结算三个平台
     * @CreateDate: Created in 2018/3/20 0020 下午 6:34
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FundSettlementTotalVo queryFundSettlementFundSettlementTotalVo() {
        return finaceSettlementHistoryMapper.queryFundSettlementTotalData();
    }

    /**
     * @param supperId
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: autoSettlementTime
     * @Decription: 根据供应商的id得到当前结算期数的拼装, 更新供应商结算表的订单历史记录eboss_finace_settlement_flag_time（手动）
     * @CreateDate: Created in 2018/1/31 0031 下午 1:44
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation finaceSettlementHistory(Long supperId, Date historyStartTime) {
        //通过供应商的id查询自动结算判断是否是首次结算的标识表
        FinaceSettlementFlagOperation finaceSettlementFlagOperation = new FinaceSettlementFlagOperation();
        //是否首次的标识 0：首次，1：非首次
        int firstFlag = 0;
        FinaceSettlementFlagOperation currentSettlmentYear = finaceSettlementHistoryMapper.getCurrentSettlmentYear(supperId);
        if (!(currentSettlmentYear == null || "".equals(currentSettlmentYear))) {
            finaceSettlementFlagOperation = currentSettlmentYear;
            //historyStartTime是本次结算的时间
            finaceSettlementFlagOperation.setSupplierSettlementLast(historyStartTime);
            firstFlag = 1;
        } else {
            finaceSettlementFlagOperation.setSupplierSettlementTime(historyStartTime);
        }
        //finaceSettlementFlagOperation用来判断是否是第一期，autoSettlementTime是eboss_finace_settlement_flag_time里面的结算时间
        FinaceSettlementHistoryOperation finace = buildStage(finaceSettlementFlagOperation, supperId, firstFlag);
        return finace;
    }


    /**
     * @param supperId
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: autoSettlementTime
     * @Decription: 根据供应商的id得到当前结算期数的拼装, 更新供应商结算表的订单历史记录eboss_finace_settlement_flag_time（自动）
     * @CreateDate: Created in 2018/1/31 0031 下午 1:44
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation autofinaceSettlementHistory(Long supperId, Date historyStartTime) {
        //通过供应商的id查询自动结算判断是否是首次结算的标识表
        FinaceSettlementFlagOperation finaceSettlementFlagOperation = new FinaceSettlementFlagOperation();
        //是否首次的标识 0：首次，1：非首次
        int firstFlag = 0;
        FinaceSettlementFlagOperation currentSettlmentYear = finaceSettlementHistoryMapper.getCurrentSettlmentYear(supperId);
        if (!(currentSettlmentYear == null || "".equals(currentSettlmentYear))) {
            finaceSettlementFlagOperation = currentSettlmentYear;
            //historyStartTime是本次结算的时间
            log.info("自动结算本次结算结束的时间为：" + historyStartTime);
            finaceSettlementFlagOperation.setSupplierSettlementLast(historyStartTime);
            firstFlag = 1;
        } else {
            finaceSettlementFlagOperation.setSupplierSettlementTime(historyStartTime);
        }
        //finaceSettlementFlagOperation用来判断是否是第一期，autoSettlementTime是eboss_finace_settlement_flag_time里面的结算时间
        FinaceSettlementHistoryOperation finace = buildStage(finaceSettlementFlagOperation, supperId, firstFlag);
        return finace;
    }

    /**
     * @param finaceSettlementFlagOperation
     * @param supperId
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation
     * @Decription: 建立结算的周期
     * @CreateDate: Created in 2018/4/14 0014 下午 7:42
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public FinaceSettlementHistoryOperation buildStage(FinaceSettlementFlagOperation finaceSettlementFlagOperation, Long supperId, int firstFlag) {
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter sdfSql = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZoneId zoneId = ZoneId.systemDefault();
        //返回的对象
        FinaceSettlementHistoryOperation finaceSettlementHistoryOperation = new FinaceSettlementHistoryOperation();
        //如果为空这表示首次进行结算，添加当前年的第一期
        if (firstFlag == 0) {
            String currentYearSettlementStage = sdf.format(LocalDateTime.now()) + "年" + 1 + "期";
            //变更(插入)表结构数据
            Date supplierSettlementTime = finaceSettlementFlagOperation.getSupplierSettlementTime();
            // Date supplierSettlementTimeFinal = LocalDateAndDateTransformUtil.localDateTimeToDate(LocalDateAndDateTransformUtil.dateToLocalDateTime(supplierSettlementTime));
            finaceSettlementHistoryMapper.updateFinaceSettlementFlagOperation(DateToStringUtil.getStringDateTime(finaceSettlementFlagOperation.getSupplierSettlementTime()), 1,
                    supperId, DateToStringUtil.getStringDateTime(new Date()));
            finaceSettlementHistoryOperation.setSettlementStage(currentYearSettlementStage);
            //第一次结算订单设置的初始值
            finaceSettlementHistoryOperation.setSettlementStartTime(finaceSettlementHistoryMapper.querySupplierProductGrounding(supperId));
            return finaceSettlementHistoryOperation;
        } else {
            //如果年份一样则期数加一
            if (sdf.format(finaceSettlementFlagOperation.getSupplierSettlementTime().
                    toInstant().atZone(zoneId).toLocalDate()).equals(sdf.format(LocalDateTime.now()))) {
                int currentStage = finaceSettlementFlagOperation.getCurrentYearSettlementStage() + 1;
                //变更(修改)表结构数据
                LocalDateTime format = finaceSettlementFlagOperation.getSupplierSettlementLast().toInstant().atZone(zoneId).toLocalDateTime();
                //sdfSql.format(format);
                finaceSettlementHistoryMapper.updateFinaceSettlementFlagOperation(sdfSql.format(format), currentStage, supperId,
                        DateToStringUtil.getStringDateTime(new Date()));
                finaceSettlementHistoryOperation.setSettlementStage(sdf.format(LocalDateTime.now()) + "年" + currentStage + "期");
                finaceSettlementHistoryOperation.setSettlementStartTime(finaceSettlementFlagOperation.getSupplierSettlementTime());
                return finaceSettlementHistoryOperation;
            } else {
                //变更(修改)表结构数据
                finaceSettlementHistoryMapper.updateFinaceSettlementFlagOperation
                        (sdfSql.format(finaceSettlementFlagOperation.getSupplierSettlementTime().toInstant().atZone(zoneId).toLocalDate()), 1, supperId,
                                DateToStringUtil.getStringDateTime(new Date()));
                finaceSettlementHistoryOperation.setSettlementStage(LocalDate.now().plusYears(1).getYear() + "年" + 1 + "期");
                finaceSettlementHistoryOperation.setSettlementStartTime(finaceSettlementFlagOperation.getSupplierSettlementTime());
                return finaceSettlementHistoryOperation;
            }
        }
    }

    /**
     * @param page
     * @ClassName: FinaceOperationServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation>
     * @Decription: 通过时间来查询结算记录列表信息
     * @CreateDate: Created in 2018/1/31 0031 下午 5:14
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<FinaceSettlementHistoryOperation> queryFinaceSettlementHistoryByChoice(PagePo<FinaceSettlementHistoryPo> page) {
        FinaceSettlementHistoryPo finaceSettlementHistoryPo = page.getFields();
        //加入当前供应商的id
        UserInfo currentUser = LocalData.getCurrentUser();
        PageHelper.startPage(page);
        List<FinaceSettlementHistoryOperation> finaceSettlementHistoryOperations =
                finaceSettlementHistoryMapper.findFinaceSettlementHistoryList(finaceSettlementHistoryPo, currentUser.getTypeId());
        PageInfo pageinfo = new PageInfo<>(finaceSettlementHistoryOperations);
        return ResultMessage.success(pageinfo);
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation>
     * @Decription: 运营商版本通过供应商id和结算期查询结算记录
     * @CreateDate: Created in 2018/2/7 0007 上午 10:15
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<FinaceSettlementHistoryOperation> queryFinaceSettlementHistoryOperation(PagePo<FinaceSettlementHistoryOperation> page) {

        FinaceSettlementHistoryOperation finaceSettlementHistoryPo = page.getFields();
        PageHelper.startPage(page);
        Example finaceSourseDataOperationExample = new Example(FinaceSettlementHistoryOperation.class);
        Example.Criteria criteria = finaceSourseDataOperationExample.createCriteria();
        //商id
        criteria.andEqualTo("merchantId", finaceSettlementHistoryPo.getMerchantId());
        //结算期数
        if (StringUtils.isNotEmpty(finaceSettlementHistoryPo.getSettlementStage())) {
            criteria.andEqualTo("settlementStage", finaceSettlementHistoryPo.getSettlementStage());
        }
        PageHelper.startPage(page);
        List<FinaceSettlementHistoryOperation> finaceSettlementHistoryOperations = finaceSettlementHistoryMapper.selectByExample(finaceSourseDataOperationExample);
        PageInfo pageinfo = new PageInfo<>(finaceSettlementHistoryOperations);
        return ResultMessage.success(pageinfo);
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription:
     * @CreateDate: Created in 2018/3/20 0020 下午 6:29
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<FinaceSourseDataOperationAndSupplierVo>> queryFinaceSettlementSupplierTotalList(PagePo<FinaceSourseDataOperationPo> page)
            throws BusinessException {
        UserInfo currentUser = LocalData.getCurrentUser();
        judgeParametersIsNull(currentUser.getTypeId());
        judgeParametersIsNull(page.getFields().getSettlementEndTime());
        judgeParametersIsNull(page.getFields().getSettlementStartTime());
        PageHelper.startPage(page);
        page.getFields().setSupplierId(currentUser.getTypeId());
        //翻译产品人群
        Map<String, String> productCrowdMap = DictionaryMapUtil.getMapByParentCode("PRODUCT_CROWD");
        List<FinaceSourseDataOperationAndSupplierVo> finaceSourseDataDealerVos = finaceSettlementHistoryMapper.queryFundSettlementFormOperation(page);
        finaceSourseDataDealerVos.stream().map(finaceSourseDataOperationAndSupplierVo -> {
            finaceSourseDataOperationAndSupplierVo.setCrowd(productCrowdMap.get(finaceSourseDataOperationAndSupplierVo.getCrowd().toString()));
            return finaceSourseDataOperationAndSupplierVo;
        }).collect(Collectors.toList());
        Long total = new PageInfo(finaceSourseDataDealerVos).getTotal();
        PageInfo pageInfo = new PageInfo<>(finaceSourseDataDealerVos);
        pageInfo.setTotal(total);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: 运营商查询结算你记录，供应商的id来源不同
     * @CreateDate: Created in 2018/3/1 0001 下午 2:51
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<FinaceSourseDataOperationAndSupplierVo>> queryFinaceSettlementOperationTotalList(PagePo<FinaceSourseDataOperationPo> page)
            throws BusinessException {
        judgeParametersIsNull(page.getFields().getSupplierId());
        judgeParametersIsNull(page.getFields().getSettlementEndTime());
        judgeParametersIsNull(page.getFields().getSettlementStartTime());
        PageHelper.startPage(page);
        List<FinaceSourseDataOperationAndSupplierVo> finaceSourseDataDealerVos = finaceSettlementHistoryMapper.queryFundSettlementFormOperation(page);
        //翻译产品人群
        Map<String, String> productCrowdMap = DictionaryMapUtil.getMapByParentCode("PRODUCT_CROWD");
        finaceSourseDataDealerVos.stream().map(finaceSourseDataOperationAndSupplierVo -> {
            finaceSourseDataOperationAndSupplierVo.setCrowd(productCrowdMap.get(finaceSourseDataOperationAndSupplierVo.getCrowd().toString()));
            return finaceSourseDataOperationAndSupplierVo;
        }).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo<>(finaceSourseDataDealerVos);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param param
     * @ClassName: FinaceFormOperationServiceImpl
     * @Return: void
     * @Decription: 校验参数是否为空
     * @CreateDate: Created in 2018/2/10 0010 下午 5:25
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public void judgeParametersIsNull(Object param) throws BusinessException {

        if (null == param || "".equals(param)) {
            throw new BusinessException("缺少必须要上传的参数！");
        }
    }

    /**
     * @param page
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription: 分销商用于远程调用
     * @CreateDate: Created in 2018/3/20 0020 下午 6:33
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<FinaceSourseDataDealerVo>> queryFinaceSettlementDealerTotalList(PagePo<FinaceSourseDataDealerPo> page) {

        PageHelper.startPage(page);
        List<FinaceSourseDataDealerVo> finaceSourseDataDealerVos = finaceSettlementHistoryMapper.queryFundSettlementFormDealer(page);
        PageInfo pageInfo = new PageInfo<>(finaceSourseDataDealerVos);
        return ResultMessage.success(pageInfo);
    }

    /**
     * @param supplierSettlementDoPo
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.entity.dealer.EarliestOrderTimeVo
     * @Decription: 供应商自动结算触发事件，真的去结算
     * @CreateDate: Created in 2018/2/28 0028 下午 7:20
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Transactional
    public Boolean queryEarliestOrderTime(SupplierSettlementDoPo supplierSettlementDoPo) {
        //查询满足条件可以进行结算的供应商
        List<SupplierSettlementInfoVo> supplierSettlementInfoVos = supplierSettlementAutoInfoMapper.querySupplierInfoList();
     /*   LocalDateTime of = LocalDateTime.of(2018, 4, 14, 18, 23, 19);
        Date date = LocalDateAndDateTransformUtil.localDateTimeToDate(of);*/
        Date date = new Date();
        supplierSettlementInfoVos.stream().map(supplierSettlementInfoVo -> {
            supplierSettlementInfoVo.setSettlementDate(date);
            return supplierSettlementInfoVo;
        }).collect(Collectors.toList());
        initFinaceSettlementHistory(supplierSettlementInfoVos);
        return true;
    }


    /**
     * @param id
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: com.cloud.drore.eboos.common.entity.dealer.EarliestOrderTimeVo
     * @Decription: 通过供应商id获取是否存在结算按钮
     * @CreateDate: Created in 2018/2/28 0028 下午 7:20
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public Boolean queryEarliestOrderFlag(Long id) {
        //通过供应商id判断是否有点击过自动结算
        SupplierSelectHistoryRecenetTimeVo supplierSettlementCount = finaceSettlementHistoryMapper.querySupplierSettlementCount(id);
        if (supplierSettlementCount.getSupplierSettlementFlag() > 0) {
            return false;
        }
        return true;
    }

    /**
     * @param i
     * @ClassName: FinaceSettlementHistoryServiceImpl
     * @Return: void
     * @Decription: 供应商结算的时候，启动定时任务
     * @CreateDate: Created in 2018/3/2 0002 下午 4:07
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public void addJob(int i, Long supperId) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.initialize();
        //定时任务可执行的
        ScheduleApplication dynamicScheduleTaskSecond = new ScheduleApplication();
        dynamicScheduleTaskSecond.startCron1();
        String cycleTime = "* * * " + i + " * ?";
        Date date = new Date();
        //每个月或者每两个月执行一次
        log.info(LocalDateTime.now() + "开启id为" + supperId + "的定时任务");
        threadPoolTaskScheduler.schedule(() -> {
            try {
                System.out.println("1");
                //initFinaceSettlementHistory(supperId, date);
            } catch (Exception e) {
                log.info(LocalDateTime.now() + "定时任务执行失败");

                e.printStackTrace();
            }
        }, new CronTrigger(cycleTime));
    }
}
